#include "epd_show.h"
#include "appconfig.h"

#define USEING_DEBUG_EPD
#ifdef USEING_DEBUG_EPD
#define rt_kprintf       rt_kprintf
#else
#define rt_kprintf(...)       
#endif


static rt_event_t showEvt;
static rt_uint8_t progress = 0;

rt_uint8_t* get_progress(void)
{
	return (rt_uint8_t*)&progress;
}

rt_err_t send_epd_mail(rt_uint32_t evt)
{
	return rt_event_send(showEvt, evt);
}

int open_lcd_power(void)
{
	rt_pin_mode(LCD_POWR_PIN,PIN_MODE_OUTPUT);
	rt_pin_write(LCD_POWR_PIN, 1);

	return 0;
}
INIT_DEVICE_EXPORT(open_lcd_power);


void epd_show_thread_entry(void *arg)
{
	rt_uint32_t evt = 0;
	rt_kprintf("%s\n\r",__FUNCTION__);
	rt_thread_entry_work(rt_thread_self());
	
	while(1)
	{
		rt_err_t result;

		rt_thread_entry_sleep(rt_thread_self());
		result = rt_event_recv(showEvt,EPD_EVT_FULL|EPD_EVT_PART|EPD_EVT_UPDATE|
																				 EPD_EVT_CLOSE|EPD_EVT_FINIT|
																				 EPD_EVT_BATAlA|EPD_EVT_CSQAlA|
																				 EPD_EVT_SIMAlA|EPD_EVT_TCPAlA|
																				 EPD_EVT_NoneSIMALA,
																				 RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,
																				 3600,&evt);
		rt_thread_entry_work(rt_thread_self());
		if(result == RT_EOK)
		{
			if(evt & EPD_EVT_FULL)//全屏更新
			{
				rt_uint8_t *buf;
				rt_kprintf("EPD_EVT_FULL\n");
				buf = show_buffer_get();
				epd_show_update(2,1,buf,RT_NULL);
			}
			else if(evt & EPD_EVT_PART)//部分更新
			{
				rt_uint8_t *buf;
				rt_kprintf("EPD_EVT_PART\n");
				buf = show_buffer_get();
				if(is_key_evt_wakeup() == 1)//按键唤醒
				{
					eeprom_load_IDdata(buf);
				}
				else
				{
					eeprom_load_data(buf);
				}
				
				epd_update_hint(buf);//更新提示
			}
			else if(evt & EPD_EVT_FINIT)//全屏初始化
			{
				rt_kprintf("EPD_EVT_FINIT\n");
				epd_show_init();
			}
			else if(evt & EPD_EVT_UPDATE)//更新进度
			{
				rt_uint8_t *value;

				rt_kprintf("EPD_EVT_UPDATE\n");
				value = get_progress();
				epd_update_progress(*value);
			}
			else if(evt & EPD_EVT_CLOSE)
			{
				rt_kprintf("EPD_EVT_UPDATE\n");
				epd_module_close();
			}
			else if(evt & EPD_EVT_BATAlA)//电池低压提示
			{
				rt_uint8_t *buf = RT_NULL;
				
				rt_kprintf("EPD_EVT_BATAlA\n");
				buf = show_buffer_get();
				epd_bat_alarm_hint(buf);
			}
			else if(evt & EPD_EVT_CSQAlA)//信号太差
			{
				rt_uint8_t *buf = RT_NULL;
				
				rt_kprintf("EPD_EVT_CSQAlA\n");
				buf = show_buffer_get();
				epd_csq_alarm_hint(buf);
			}
			else if(evt & EPD_EVT_SIMAlA)//sim卡欠费
			{
				rt_uint8_t *buf = RT_NULL;
				
				rt_kprintf("EPD_EVT_SIMAlA\n");
				buf = show_buffer_get();
				epd_sim_alarm_hint(buf);
			}
			else if(evt & EPD_EVT_TCPAlA)//tcp链接服务器
			{
				rt_uint8_t *buf = RT_NULL;
				
				rt_kprintf("EPD_EVT_TCPAlA\n");
				buf = show_buffer_get();
				epd_tcp_alarm_hint(buf);
			}
			else if(evt & EPD_EVT_NoneSIMALA)//tcp链接服务器
			{
				rt_uint8_t *buf = RT_NULL;
				
				rt_kprintf("EPD_EVT_NoneSIMALA\n");
				buf = show_buffer_get();
				epd_noneSIM_alarm_hint(buf);
			}
		}
	}
}

int epd_show_thread_init(void)
{
	rt_thread_t thread;

	showEvt = rt_event_create("showe", RT_IPC_FLAG_FIFO);
	RT_ASSERT(showEvt);
	
	thread = rt_thread_create("show",
							  epd_show_thread_entry,RT_NULL,
							  512,19,20);
	if(thread != RT_NULL)
	{
		rt_thread_startup(thread);
	}

	return 0;
}

INIT_APP_EXPORT(epd_show_thread_init);

